﻿using Qiniu.Http;
using Qiniu.IO.Model;
using Qiniu.IO;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.Common;
using System.Net.Sockets;

namespace MCPSSETest.Utils
{
    public static class QiNiuHelper
    {

        /// <summary>
        /// 简单上传-上传字节数据
        /// </summary>
        public static string UploadData(byte[] bytes)
        {
            Qiniu.Common.Config.AutoZone("eJ6mggA3RW2XpB_rEwlnds1IvnyMGyipMtFf-C6y", "lstgtest", true);
            // 生成(上传)凭证时需要使用此Mac
            // 这个示例单独使用了一个Settings类，其中包含AccessKey和SecretKey
            // 实际应用中，请自行设置您的AccessKey和SecretKey
            Mac mac = new Mac("eJ6mggA3RW2XpB_rEwlnds1IvnyMGyipMtFf-C6y", "91eQy5dTW_V3evxn8owugOKJ89_gP-YpK4qGW-rL");
            string bucket = "lstgtest";
            string saveKey = "myfile"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello World!");
            // 上传策略，参见 
            // https://developer.qiniu.com/kodo/manual/put-policy
            PutPolicy putPolicy = new PutPolicy();
            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖)，请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope = bucket;
            // 上传策略有效期(对应于生成的凭证的有效期)          
            putPolicy.SetExpires(3600);
            // 上传到云端多少天后自动删除该文件，如果不设置（即保持默认默认）则不删除
            putPolicy.DeleteAfterDays = 1;
            // 生成上传凭证，参见
            // https://developer.qiniu.com/kodo/manual/upload-token            
            string jstr = putPolicy.ToJsonString();
            string token = Auth.CreateUploadToken(mac, jstr);
            FormUploader fu = new FormUploader();
            HttpResult result = fu.UploadData(bytes, saveKey, token);
            Console.WriteLine(result);
            return $"图片的链接是： http://syrg6mkl5.hn-bkt.clouddn.com/{saveKey}";
        }
    }
}
